<?php
/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */

class BookController extends Zend_Controller_Action
{
    private $_redirector = null;
    
    private $_newbooks_page_limit   = 6;
    
    private $_allbooks_page_limit   = 18;
    
    private $_readinglist_page_limit = 18;
    
    private $_review_page_limit     = 3;
    
    private $_newbooks_day_limit    = 30;
    
    private $_notes_page_limit      = 3;
    
    public function init()
    {
        $this->_redirector = $this->_helper->getHelper('Redirector');
        $this->stylesBase  = $this->view->baseUrl() . '/styles/';
    }
    
    /**
     * This will validate our session and the requested action;
     *
     */
    public function preDispatch()
    {
        // Check our identity
        if(Zend_Auth::getInstance()->hasIdentity())
        {
            // Check the requestion action
            switch ( $this->getRequest()->getActionName())
            {
                case 'index':
                case 'search':
                case 'add':
                case 'edit':
                case 'delete':
                case 'addtolist':  
                case 'removefromlist':
                case 'view':
                case 'viewchapter':
                case 'viewarticle':
                case 'ajaxrequest':
                case 'addreview':
                case 'addnote':
                    // -- These actions are allowed, nothing to do -- //
                    break;
                default:
                    // -- Invalid action requested, refer to homepage -- //
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        }
        else
        {
            // -- No session, refer to loginpage -- //
            $this->_helper->redirector('login', 'user');
        }
    }
    
    /**
     * This action will handle all ajax-requests
     */
    public function ajaxrequestAction()
    {
        // Disable the layout
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        // Get current user
        $user   = Zend_Auth::getInstance()->getStorage()->read();
        
        // These are default pager variables
        $current_page   = (string)$this->getRequest()->getParam('p');
        $pager_base     = $this->getRequest()->getBaseUrl()."/book/index/";
        
        // Get the request and do something accordingly
        $request  = (string)$this->getRequest()->getParam('req');
        switch($request)
        {
            case "pagenewbooks":
                $pager_var      = "np";
                $html[]         = $this->_loopPartial('books-block-book', $this->_getBooksByRecent($this->_newbooks_day_limit, $current_page, $this->_newbooks_page_limit));
                $html[]         = $this->_fetchPager($current_page, ($this->_countBooksByRecent($this->_newbooks_day_limit)/$this->_newbooks_page_limit), $pager_base, $pager_var);
                $result         = implode("\n", $html);
                break;
            case "pagemybooks":
                $pager_var      = "bp";
                $html[]         = $this->_loopPartial('books-block-book', $this->_getBooksByUserId($user->Id, $current_page, $this->_readinglist_page_limit));
                $html[]         = $this->_fetchPager($current_page, ($this->_countBooksByUserId($user->Id)/$this->_readinglist_page_limit), $pager_base, $pager_var);
                $result         = implode("\n", $html);
                break;
            case "pageallbooks":
                $pager_var      = "ap";
                $html[]         = $this->_loopPartial('books-block-book', $this->_getBooks($current_page, $this->_allbooks_page_limit));
                $html[]         = $this->_fetchPager($current_page, ($this->_countBooks()/$this->_allbooks_page_limit), $pager_base, $pager_var);
                $result         = implode("\n", $html);
                break;
            case "pagereviews":
                $book_id      = (string)$this->getRequest()->getParam('id');
                $pager_var      = "rp";
                $html[]         = $this->_loopPartial('block-book-review', $this->_getReviewsByBookId($book_id, $current_page, $this->_review_page_limit));
                $html[]         = $this->_fetchPager($current_page, ($this->_countReviewsByBookId($book_id)/$this->_review_page_limit), $pager_base, $pager_var);
                $result         = implode("\n", $html);
                break;
            case "pagenotes":
                $article_id     = (string)$this->getRequest()->getParam('id');
                $pager_var      = "np";
                $html[]         = $this->_loopPartial('block-article-note', $this->_getNotesByArticleId($article_id, $current_page, $this->_notes_page_limit));
                $html[]         = $this->_fetchPager($current_page, ($this->_countNotesByArticleId($article_id)/$this->_notes_page_limit), $pager_base, $pager_var);
                $result         = implode("\n", $html);
                break;
            default:
                $result = false;
                break;
        }
        
        // output result
        echo $result; 
    }
    
    /**
     * This is the main action.
     * It will display the Books' tabbed view, with Summary and My Readinglist
     *
     */
    public function indexAction()
    {
        // Set up the view
        $view = $this->_getView('Books');
        // Get the logged in User-object, and get other 
        $user   = Zend_Auth::getInstance()->getStorage()->read();
        // Pass some variables to our view
        $view->user     = $user;
        // Pager variables
        $pager_base     = $this->getRequest()->getBaseUrl()."/book/index/";
        // Set NEWBOOKS block
        $newb_count             = $this->_countBooksByRecent($this->_newbooks_day_limit);
        if($newb_count > 0)
        {
            $newb_pager_var     = "np";
            $newb_current_page  = (string)$this->getRequest()->getParam($newb_pager_var);
            $newb_current_page  = ($newb_current_page > 0 ? $newb_current_page : 1);
            $view->newb_partial = $this->_loopPartial('books-block-book', $this->_getBooksByRecent($this->_newbooks_day_limit, $newb_current_page, $this->_newbooks_page_limit));
            $view->newb_pager   = $this->_fetchPager($newb_current_page, ($newb_count/$this->_newbooks_page_limit), $pager_base, $newb_pager_var);
        }
        else
        {
            $view->newb_partial = "<p>No new books have been added to our website in the last ".$this->_newbooks_day_limit." days.</p>";
            $view->newb_pager   = null;
        }
        // Set READINGLIST block
        $book_count             = $this->_countBooksByUserId($user->Id);
        if($book_count > 0)
        {
            $book_pager_var     = "bp";
            $book_current_page  = (string)$this->getRequest()->getParam($book_pager_var);
            $book_current_page  = ($book_current_page > 0 ? $book_current_page : 1);
            $view->book_partial = $this->_loopPartial('books-block-book', $this->_getBooksByUserId($user->Id, $book_current_page, $this->_readinglist_page_limit));
            $view->book_pager   = $this->_fetchPager($book_current_page, ($book_count/$this->_readinglist_page_limit), $pager_base, $book_pager_var);
        }
        else
        {
            $view->book_partial = $view->partial('partials/add-book.phtml', array());
            $view->book_pager   = null;
        }
        // Set ALL BOOKS block
        $allb_count             = $this->_countBooks();
        if($allb_count > 0)
        {
            $allb_pager_var     = "np";
            $allb_current_page  = (string)$this->getRequest()->getParam($allb_pager_var);
            $allb_current_page  = ($allb_current_page > 0 ? $allb_current_page : 1);
            $view->allb_partial = $this->_loopPartial('books-block-book', $this->_getBooks($allb_current_page, $this->_allbooks_page_limit));
            $view->allb_pager   = $this->_fetchPager($allb_current_page, ($allb_count/$this->_allbooks_page_limit), $pager_base, $allb_pager_var);
        }
        else
        {
            $view->allb_partial = "<p>There are currently no books in our database.</p>";
            $view->allb_pager   = null;
        }
        /**
         * @todo add recommend block
        // Set RECOMMENDED block
        $crse_pager_var     = "cp";
        $crse_current_page  = (int)$this->getRequest()->getParam($crse_pager_var);
        $crse_current_page  = ($crse_current_page > 0 ? $crse_current_page : 1);
        $view->crse_partial = $this->_loopPartial('books-block-book', $this->_getCoursesByUserId($user->Id, $crse_current_page, $this->_courses_page_limit));
        $view->crse_pager   = $this->_fetchPager($crse_current_page, ($this->_countCoursesByUserId($user->Id)/$this->_courses_page_limit), $pager_base, $crse_pager_var);
        */
    }
    
    /**
     * This will let a user browse an entire book, including chapters and articles
     * 
     */
    public function viewAction()
    {
        // Get view
        $view   = $this->_getView("View book");
        // Get parameters
        $book_id        = (string)$this->getRequest()->getParam('id');
        $chapter_id     = (string)$this->getRequest()->getParam('chapter');
        $article_id     = (string)$this->getRequest()->getParam('article');
        // Pager variables
        $pager_base     = $this->getRequest()->getBaseUrl()."/book/view/id/".$book_id."/";
        // Load objects
        $user       = Zend_Auth::getInstance()->getStorage()->read();
        $book       = $this->_getBookById($book_id);
        $chapter    = $this->_getChapterById($chapter_id);
        $article    = $this->_getArticleById($article_id);
        // Pass variables to view
        $view->user     = $user;
        $view->book     = $book;
        $view->chapter  = $chapter;
        $view->article  = $article;
        // Breadcrumb modification
        $page_book   = $view->navigation()->getContainer()->findOneByLabel('View book');
        $page_book->setLabel($book->Title)->setParams(array('id' => $book_id));
        if($chapter->Id != null)
        {
            $page_book->addPage(array('label'=>'View chapter', 'uri'=>$page_book->getHref().'/chapter/'.$chapter_id, 'active'=>'1'));

            $page_chapt = $page_book->findOneByLabel('View chapter');
            $page_chapt->setLabel($chapter->Name);
            if($article->Id != null)
            {
                $page_chapt->addPage(array('label'=>'View article', 'uri'=>$page_chapt->getHref().'/article/'.$article_id, 'active'=>'1'));

                $page_article = $page_book->findOneByLabel('View article');
                $page_article->setLabel($article->Name);
                
                // Add notes block
                $pager_base         = $pager_base.'chapter/'.$chapter->Id.'/article/'.$article->Id.'/';
                $note_count         = $this->_countNotesByArticleId($article->Id);
                if($note_count > 0)
                {
                    $note_pager_var     = "np";
                    $note_current_page  = (string)$this->getRequest()->getParam($note_pager_var);
                    $note_current_page  = ($note_current_page > 0 ? $note_current_page : 1);
                    $view->note_partial = $this->_loopPartial('block-article-note', $this->_getNotesByArticleId($article->Id, $note_current_page, $this->_notes_page_limit));
                    $view->note_pager   = $this->_fetchPager($note_current_page, ($note_count/$this->_notes_page_limit), $pager_base, $note_pager_var);
                }
                else
                {
                    $view->note_partial = "<p>You have not added any notes to this article.</p>";
                    $view->note_pager   = null;
                }
            }
        }
        else
        {
            // Add review block
            $revw_count         = $this->_countReviewsByBookId($book_id);
            if($revw_count > 0)
            {
                $revw_pager_var     = "rp";
                $revw_current_page  = (string)$this->getRequest()->getParam($revw_pager_var);
                $revw_current_page  = ($revw_current_page > 0 ? $revw_current_page : 1);
                $view->revw_partial = $this->_loopPartial('block-book-review', $this->_getReviewsByBookId($book_id, $revw_current_page, $this->_review_page_limit));
                $view->revw_pager   = $this->_fetchPager($revw_current_page, ($revw_count/$this->_review_page_limit), $pager_base, $revw_pager_var);
            }
            else
            {
                $view->revw_partial = "<p>There are no reviews for this book.</p>";
                $view->revw_pager   = null;
            }
        }
    }
    
    /**
     * This will let a user add a book to his readinglist
     *
     */
    public function addtolistAction()
    {
        $user     = Zend_Auth::getInstance()->getStorage()->read();
        $book_id  = (string)$this->getRequest()->getParam('id');
        $this->_addBookToReadinglist($book_id, $user->Id);
        // Redirect to book
        $this->_redirector->gotoSimple(
                               'view',
                               'book',
                               null,
                               array('id' => $book_id)
        );
    }
    
    /**
     * This will let a user delete a book from his readinglist
     *
     */
    public function removefromlistAction()
    {
        $user     = Zend_Auth::getInstance()->getStorage()->read();
        $book_id  = (string)$this->getRequest()->getParam('id');
        $this->_removeBookFromReadinglist($book_id, $user->Id);
        // Redirect to book index
        $this->_redirector->gotoSimple(
                               'index',
                               'book',
                               null,
                               array()
        );
    }
    
    /**
     * Add a review to a book
     */
    public function addreviewAction()
    {
        // Disable the layout
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        // Get the current user
        $user     = Zend_Auth::getInstance()->getStorage()->read();
        // Get the form
        $form = $this->_getAddReviewForm();
        
        if($this->getRequest()->isPost()) 
	{
            $ajaxresult = array();
            $formdata   = $this->_request->getPost();
            if($form->isValid($formdata)) 
            {
                $mapper = new Application_Model_ReviewMapper();
                $values = $form->getValues();
                $review = new Application_Model_Review();
                
                $review->Subject        = $values['review_subject'];
                $review->Text           = $values['review_text'];
                $review->AuthorId       = $user->Id;
                $review->BookId         = $values['book_id'];
         
                $review_id    = $mapper->save($review);
                
                if($review_id != null)
                {
                    $ajaxresult['result']   = true;
                }
                else
                {
                    $ajaxresult['result']   = false;
                    $ajaxresult['error']    = '<p>We could not save the review due to an internal error.</p>';
                    $ajaxresult['form']     = $form->render();
                }
            }
            else
            {
                $ajaxresult['error']    = '<p>You have entered some invalid data.</p>';
                $ajaxresult['result']   = false;
                $ajaxresult['form']     = $form->render();
            }
            
            $this->_helper->json($ajaxresult);
        }
        else
        {                  
            $book_id = (string)$this->getRequest()->getParam('id');

            $form->populate(array(
                    'book_id'   => $book_id
            ));
            
            echo $form;
        }
    }
    
    /**
     * Add a note to a book
     */
    public function addnoteAction()
    {
        // Disable the layout
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        // Get the current user
        $user     = Zend_Auth::getInstance()->getStorage()->read();
        // Get the form
        $form = $this->_getAddNoteForm();
        
        if($this->getRequest()->isPost()) 
	{
            $ajaxresult = array();
            $formdata   = $this->_request->getPost();
            if($form->isValid($formdata)) 
            {
                $mapper = new Application_Model_CommentMapper();
                $values = $form->getValues();
                $comment = new Application_Model_Comment();
                
                $comment->Subject        = $values['note_subject'];
                $comment->Text           = $values['note_text'];
                $comment->AuthorId       = $user->Id;
                $comment->ArticleId      = $values['article_id'];
         
                $comment_id    = $mapper->save($comment);
                
                if($comment_id != null)
                {
                    $ajaxresult['result']   = true;
                }
                else
                {
                    $ajaxresult['result']   = false;
                    $ajaxresult['error']    = '<p>We could not save the note due to an internal error.</p>';
                    $ajaxresult['form']     = $form->render();
                }
            }
            else
            {
                $ajaxresult['error']    = '<p>You have entered some invalid data.</p>';
                $ajaxresult['result']   = false;
                $ajaxresult['form']     = $form->render();
            }
            
            $this->_helper->json($ajaxresult);
        }
        else
        {                  
            $article_id = (string)$this->getRequest()->getParam('id');

            $form->populate(array(
                    'article_id'   => $article_id
            ));
            
            echo $form;
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function searchAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $books= $this->_getBookByName($this->_getParam('term'));                
      
        $result = array();    
 
        foreach ($books as $book) {            
            $result[] = $book->Title;
        }
        
        $this->_helper->json($result);
    }
    
    
    public function editAction()
    {
        $view = $this->_getView('Edit Book');
        $form = $this->_getEditBookForm();
        
        if($this->getRequest()->isPost()) 
	{            
            $formData  = $this->_request->getPost();
            if($form->isValid($formData)) 
            {
                $mapper = new Application_Model_BookMapper();    
                $view   = $this->_getView('Fout - Toevoegen');                
                $values = $form->getValues();
                $book   = new Application_Model_Book();
                
                $book->Id           = $values['book_id'];
                $book->Title        = $values['book_title'];
                $book->Subtitle     = $values['book_subtitle'];
                $book->Description  = $values['book_description'];
         
                $book_id    = $mapper->save($book);
                $course_id  = $values['book_course'];
                
                if($book_id != null && $course_id != null)
                    $mapper->addBookToCourse($book_id, $course_id);

                return $this->_helper->redirector('index', 'book');             
            }
        }
        else
        {                  
                $book_id = (string)$this->getRequest()->getParam('id');
                $book    = $this->_getBookById($book_id);
                
                $form->populate(array(
                        'book_id'               => $book->Id,    
                        'book_title'            => $book->Title,
                        'book_subtitle'         => $book->Subtitle,
                        'book_description'      => $book->Description,
                ));
                        
                $view->form = $form;     
        }
    }
    
        
    public function deleteAction()
    {
        $id = (string)$this->getRequest()->getParam('id');
        $bookMapper = new Application_Model_BookMapper();
        $bookMapper->delete($id);
        $this->_helper->redirector('index', 'book');
    }

    /**
     * @return Application_Form_EditCourse
     */
    private function _getEditBookForm()
    {   
        return new Application_Form_EditBook(array(
            'action' => $this->view->baseUrl() . '/book/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @param string $name
     * @return Array
     */
    private function _getBookById($id)
    {   
        $mapper = new Application_Model_BookMapper();
        return $mapper->fetchById($id);
    }
    
    /**
     * @param string $name
     * @return Array
     */
    private function _getChapterById($id)
    {   
        $mapper = new Application_Model_ChapterMapper();
        return $mapper->fetchById($id);
    }
    
    /**
     * @param string $name
     * @return Array
     */
    private function _getArticleById($id)
    {   
        $mapper = new Application_Model_ArticleMapper();
        return $mapper->fetchById($id);
    }
    
       
    /**
     * @return Application_Form_Registration
     */
    private function _getBookDetailsForm()
    {
        return new Application_Form_NameSearch(array(
            'action' => $this->view->baseUrl('book/index'),
            'method' => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getBookByName($bookTitle)
    {
        
        $bookMapper = new Application_Model_BookMapper();
        $bookArray = $bookMapper->fetchByName($bookTitle);  

        return $bookArray;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * @return Application_Form_AddReview
     */
    private function _getAddReviewForm()
    {   
        return new Application_Form_AddReview(array(
            'action' => $this->view->baseUrl() . '/book/addreview/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Application_Form_AddNote
     */
    private function _getAddNoteForm()
    {   
        return new Application_Form_AddNote(array(
            'action' => $this->view->baseUrl() . '/book/addnote/',
            'post'   => 'post',
        ));
    }
    
    /**
     * Get all books
     *
     * @param integer $page_nr      If you wish to use paging, pass the page-number. To
     *                              disable paging, pass null as value (default) and 
     *                              return all rows.
     * @param integer $nr_results   The number of results per page, defaults to 10.
     * @return Array(Application_Model_Book)
     *
     */
    private function _getBooks($page_nr = null, $nr_results = 0)
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->fetchAll($page_nr, $nr_results);
    }
    
    /**
     * Returns the number of books
     * 
     * @return int 
     */
    private function _countBooks()
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->getCount();
    }
    
    /**
     * Get the user's readinglist
     *
     * @param integer $days         The number of days to fetch back to
     * @param integer $page_nr      If you wish to use paging, pass the page-number. To
     *                              disable paging, pass null as value (default) and 
     *                              return all rows.
     * @param integer $nr_results   The number of results per page, defaults to 10.
     * @return Array(Application_Model_Book)
     */
    private function _getBooksByRecent($days, $page_nr = null, $nr_results = 0)
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->fetchByRecent($days, $page_nr, $nr_results);
    }

    /**
     * Returns the number of recent books
     *
     * @param int $days     The number of days to fetch back to
     * @return integer
     */
    private function _countBooksByRecent($days)
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->getCountByRecent($days);
    }
    
    /**
     * Get the user's readinglist
     *
     * @param integer $user_id
     * @param integer $page_nr      If you wish to use paging, pass the page-number. To
     *                              disable paging, pass null as value (default) and 
     *                              return all rows.
     * @param integer $nr_results   The number of results per page, defaults to 10.
     * @return Array(Application_Model_Book)
     */
    private function _getBooksByUserId($user_id, $page_nr = null, $nr_results = 0)
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->fetchByUserId($user_id, $page_nr, $nr_results);
    }

    /**
     * Returns the number of books the user has in his readinglist
     *
     * @param int $user_id
     * @return int 
     */
    private function _countBooksByUserId($user_id)
    {
        $mapper = new Application_Model_BookMapper();
        return $mapper->getCountByUserId($user_id);
    }
    
    /**
     * Get the reviews for a book
     *
     * @param integer $book_id
     * @param integer $page_nr      If you wish to use paging, pass the page-number. To
     *                              disable paging, pass null as value (default) and 
     *                              return all rows.
     * @param integer $nr_results   The number of results per page, defaults to 10.
     * @return Array(Application_Model_Book)
     */
    private function _getReviewsByBookId($book_id, $page_nr = null, $nr_results = 0)
    {
        $mapper = new Application_Model_ReviewMapper();
        return $mapper->fetchByBookId($book_id, $page_nr, $nr_results);
    }

    /**
     * Count the reviews for a book
     *
     * @param integer $book_id
     * @return integer
     */
    private function _countReviewsByBookId($book_id)
    {
        $mapper = new Application_Model_ReviewMapper();
        return $mapper->getCountByBookId($book_id);
    }
    
    /**
     * Get the notes for an article
     *
     * @param integer $article_id
     * @param integer $page_nr      If you wish to use paging, pass the page-number. To
     *                              disable paging, pass null as value (default) and 
     *                              return all rows.
     * @param integer $nr_results   The number of results per page, defaults to 10.
     * @return Array(Application_Model_Book)
     */
    private function _getNotesByArticleId($article_id, $page_nr = null, $nr_results = 0)
    {
        $mapper = new Application_Model_CommentMapper();
        return $mapper->fetchByArticleId($article_id, $page_nr, $nr_results);
    }

    /**
     * Count the notes for an article
     *
     * @param integer $article_id
     * @return integer
     */
    private function _countNotesByArticleId($article_id)
    {
        $mapper = new Application_Model_CommentMapper();
        return $mapper->getCountByArticleId($article_id);
    }
    
    /**
     * Add a book the a user's readinglist
     * 
     * @param integer $book_id
     * @param integer $user_id 
     */
    private function _addBookToReadinglist($book_id, $user_id)
    {
        $mapper = new Application_Model_UserMapper();
        $mapper->addBookToUser($book_id, $user_id);
    }
    
    /**
     * Removes a book from the user's readinglist
     *
     * @param integer $book_id
     * @param integer $user_id
     *
     */
    private function _removeBookFromReadinglist($book_id, $user_id)
    {
        $mapper = new Application_Model_UserMapper();
        $mapper->deleteBookFromUser($book_id, $user_id);
    }
    
    /**
     * Loops through a list of custom objects, passing each object to the partial.
     * The end result is a html-render of the all the partials.
     * 
     * @param string $partial
     * @param Array $objects
     * @return string
     */
    private function _loopPartial($partial, $objects, $extravars=null)
    {
        $result = array();
        $count  = 0;
        if(count($objects))
        {
            foreach($objects as $object)
            {
                $count++;
                $vars       = array(
                    'object'    => $object,
                    'count'     => $count,
                );
                
                if(is_array($extravars))
                    $vars = array_merge($extravars, $vars);
                
                $result[]   = $this->_getView("")->partial('partials/'.$partial.'.phtml', $vars);
            }
        }
        
        return implode("\n", $result);
    }
    
    /**
     * Get the HTML pager for a list that requires pagination
     * 
     * @param int $page_nr
     * @param int $total_pages
     * @param string $base_url
     * @param string $page_var
     * @return string 
     */
    private function _fetchPager($current_page, $total_pages, $base_url, $page_var)
    {
        $view   = $this->_getView("");
        $vars   = array(
            'cur_page'      => $current_page,
            'nr_of_pages'   => ceil($total_pages),
            'base_url'      => $base_url,
            'page_var'      => $page_var,
        );
        
        return $view->partial('partials/pager.phtml', $vars);
    }
    
    /**
     * This returns the view for this controller.
     *
     * @param string $title
     * @return Zend_View
     *
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'PREPEND');

        return $view;
    }


}

